package middleware

import (
	"net/http"

	"go.opentelemetry.io/otel"
)

// TracingMiddleware
// Title    			TracingMiddleware
// Description   		追踪中间件
// Author      			alittlexincan@163.com       					2024/07/17 11:36
// Update    			alittlexincan@163.com       					2024/07/17 11:36
// Param    			next 					http.Handler			下一个处理程序
// Return    			http.Handler     								处理程序
func TracingMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()
		tracer := otel.GetTracerProvider().Tracer("http")
		// 创建根Span
		ctx, span := tracer.Start(ctx, "http-request")
		defer span.End()
		// 将带有Span的上下文传递给后续处理
		next.ServeHTTP(w, r.WithContext(ctx))
	})
}
